package com.example.myapplication.tack_22;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.tack_22.adapter.MetroAdapter;
import com.example.myapplication.tack_22.data.MetroData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;

public class RouteDetailsActivity extends AppCompatActivity {
    private ImageView exit;
    private TextView first;
    private TextView runStationsName;
    private TextView startTime;
    private TextView end;
    private TextView endTime;
    private TextView km;
    private RecyclerView route;
    private Intent intent;
    private String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_22_activity_route_details);
        init();
        exit_();
        getMetron(id);


    }

    private void fill(MetroData.DataDTO metro) {
        first.setText("起点"+metro.getFirst());
        runStationsName.setText(metro.getRunStationsName());
        startTime.setText("起点时间:"+metro.getStartTime());
        end.setText("终点"+metro.getEnd());
        km.setText(metro.getKm()+"km");
        endTime.setText("末班时间"+metro.getEndTime());

    }

    private void getMetron(String id) {
        String url = "prod-api/api/metro/line/"+id;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, MetroData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                MetroData metro = (MetroData)obj;

                if(metro.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            fill(metro.getData());
                            route.setAdapter(new MetroAdapter(metro.getData().getMetroStepList(),RouteDetailsActivity.this,metro.getData().getRunStationsName()));
                            route.setLayoutManager(new LinearLayoutManager(RouteDetailsActivity.this,GridLayoutManager.HORIZONTAL,false));
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouteDetailsActivity.this.finish();
            }
        });
    }

    private void init() {
        intent = getIntent();
        id = intent.getStringExtra(Common.COMMON);
        exit = findViewById(R.id.exit);
        first = findViewById(R.id.first);
        runStationsName = findViewById(R.id.runStationsName);
        end = findViewById(R.id.end);
        startTime = findViewById(R.id.startTime);
        endTime = findViewById(R.id.endTime);
        km = findViewById(R.id.km);
        route = findViewById(R.id.route);
    }
}